#include "WindowState.h"
#include "..\Buffer.h"


namespace Zaf {
namespace Mwl {
namespace __Internal {



HWND WindowCreatedState::GetParentHandle() const {

	HWND hParent = ::GetParent(m_hwnd);

	if (hParent == NULL) {
		hParent = ::GetWindow(m_hwnd, GW_OWNER);
	}

	return hParent;
}


void WindowCreatedState::GetSize(unsigned int& width, unsigned int& height) const {

	RECT rect;
	GetWindowRect(m_hwnd, &rect);

	width = rect.right - rect.left;
	height = rect.bottom - rect.top;
}


void WindowCreatedState::GetPosition(unsigned int& x, unsigned int& y) const {

	RECT rect;
	GetWindowRect(m_hwnd, &rect);

	POINT point;
	point.x = rect.left;
	point.y = rect.top;

	if ((GetStyle() & WS_CHILD) != 0) {

		HWND hParent = ::GetParent(m_hwnd);
		ScreenToClient(hParent, &point);
	}

	x = point.x;
	y = point.y;
}



std::wstring WindowCreatedState::GetText() const {

	int textLength = GetWindowTextLength(m_hwnd);

	Zaf::Buffer<wchar_t> buffer(textLength + 1);

	GetWindowText(m_hwnd, buffer, buffer.GetLength());

	return buffer.Get();
}



WindowNotCreatedState::WindowNotCreatedState() {

	m_style = 0;
	m_exStyle = 0;
	m_width = 0;
	m_height = 0;
	m_x = 0;
	m_y = 0;
	m_parent = NULL;
}


}
}
}